#include "StdAfx.h"
#include "SchemaInterpreter.h"
#include "JsonSchema.h"
#include "Types.h"
#include "TimestampGenerator.h"
SchemaInterpreter::SchemaInterpreter(void)
{

}


SchemaInterpreter::~SchemaInterpreter(void)
{

}


void SchemaInterpreter::generateRandomDocumentBySchema(boost::shared_ptr<JsonSchema> jsonSchema, Document& outputDocument)
{
	
	Document & schemaDocument = jsonSchema->getSchemaDocument();
	Document & propertiesDocument = schemaDocument.getField(SCHEMA_PROPERTIES).embeddedObject();
	generateRandomObject(propertiesDocument,outputDocument);

	
}
void SchemaInterpreter::generateRandomInteger(int& i)
{
	Timestamp t = TimestampGenerator::getCurrentTime();
	//i = t%3;
	i = 1;
}
void SchemaInterpreter::generateRandomString(std::string& str)
{
	std::string strArray[3];
	strArray[0] = "string1";
	strArray[1] = "string2";
	strArray[2] = "string3";
	Timestamp t = TimestampGenerator::getCurrentTime();
	//str = strArray[t%3];
	str = strArray[0];
}
void SchemaInterpreter::generateRandomObject(Document & propertiesDocument, Document& outputDocument)
{
	DocumentBuilder documentBuilder;
	DocumentIterator it(propertiesDocument);
	while(it.more())
	{
		DocumentElement& documentElement =  it.next();	
		std::string fieldName = documentElement.fieldName();
		Document& fieldDocument = documentElement.embeddedObject();	
		std::string fieldType = std::string(fieldDocument.getField(SCHEMA_TYPE).valuestr());
		JSONTYPE jsonType =  generateJsonType(fieldType);

		if(jsonType==JSON_TRUE)
		{
			documentBuilder.appendBool(fieldName,true);
		}
		else if(jsonType == JSON_FALSE)
		{
			documentBuilder.appendBool(fieldName,false);
		}
		else if(jsonType == JSON_ARRAY)
		{
			Document nestedDocument;
			Document & nestedItemsDocument = propertiesDocument.getField(SCHEMA_ITEMS).embeddedObject();
			generateRandomArray(nestedItemsDocument,nestedDocument);
			documentBuilder.append(fieldName,nestedDocument);
		}
		else if(jsonType == JSON_NULL)
		{
			documentBuilder.appendBool(fieldName,NULL);
		}
		else if(jsonType == JSON_NUMBER)
		{
			int i;
			generateRandomInteger(i);
			documentBuilder.append(fieldName,i);
		}
		else if(jsonType == JSON_OBJECT)
		{
			Document nestedDocument;
			Document & nestedPropertiesDocument = propertiesDocument.getField(SCHEMA_PROPERTIES).embeddedObject();
			generateRandomObject(nestedPropertiesDocument,nestedDocument);
			documentBuilder.append(fieldName,nestedDocument);
		}
		else if(jsonType == JSON_STRING)
		{
			string str;
			generateRandomString(str);
			documentBuilder.append(fieldName,str);
		}

	}
	outputDocument = documentBuilder.obj();
}
void SchemaInterpreter::generateRandomArray(Document & itemsDocument, Document& outputDocument)
{
	ArrayBuilder arrayBuilder;

	std::string fieldType = std::string(itemsDocument.getField(SCHEMA_TYPE).valuestr());
	JSONTYPE jsonType =  generateJsonType(fieldType);

	int arraySize = 3;
	if(jsonType==JSON_TRUE)
	{
		for(int i = 0 ;i<arraySize ; i++)
		{
			strstream ss;
			ss<<i;
			arrayBuilder.append(ss.str(),true);
		}
		
	}
	else if(jsonType == JSON_FALSE)
	{
		for(int i = 0 ;i<arraySize ; i++)
		{
			strstream ss;
			ss<<i;
			arrayBuilder.append(ss.str(),false);
		}
	}
	else if(jsonType == JSON_ARRAY)
	{
		for(int i = 0 ;i<arraySize ; i++)
		{
			strstream ss;
			ss<<i;
			Document nestedDocument;
			Document & nestedItemsDocument = itemsDocument.getField(SCHEMA_ITEMS).embeddedObject();
			generateRandomArray(nestedItemsDocument,nestedDocument);
			arrayBuilder.append(ss.str(),nestedDocument);
		}
		
	}
	else if(jsonType == JSON_NULL)
	{
		for(int i = 0 ;i<arraySize ; i++)
		{
			strstream ss;
			ss<<i;
			arrayBuilder.append(ss.str(),NULL);
		}
	}
	else if(jsonType == JSON_NUMBER)
	{
		int random;
		for(int i = 0 ;i<arraySize ; i++)
		{
			generateRandomInteger(random);
			strstream ss;
			ss<<i;
			arrayBuilder.append(ss.str(),random);
		}
		
	}
	else if(jsonType == JSON_OBJECT)
	{
		for(int i = 0 ;i<arraySize ; i++)
		{
			
			strstream ss;
			ss<<i;
			Document nestedDocument;
			Document & nestedPropertiesDocument = itemsDocument.getField(SCHEMA_PROPERTIES).embeddedObject();
			generateRandomObject(nestedPropertiesDocument,nestedDocument);
			arrayBuilder.append(ss.str(),nestedDocument);
		}
		
	}
	else if(jsonType == JSON_STRING)
	{
		for(int i = 0 ;i<arraySize ; i++)
		{
			strstream ss;
			ss<<i;
			string str;
			generateRandomString(str);
			arrayBuilder.append(ss.str(),str);
		}
		
	}
	outputDocument = arrayBuilder.obj();
}
bool SchemaInterpreter::checkDocumentSatisfiedSchema(Document& document, boost::shared_ptr<JsonSchema>jsonSchema)
{
	return true;
}